package com.mine.frame.mylandemo.mvp.presenter

import com.mine.frame.common.base.BasePresenter
import com.mine.frame.mylandemo.entity.forick.ControlBean
import com.mine.frame.mylandemo.mvp.iview.ITranslationDesk
import com.third.device.mg.serialport.MGSerialPort
import com.third.device.mg.serialport.utils.toUInt16H

/**
 * @Date 2022/2/12
 * Description:
 */
class TranslationDeskPresenter(mView: ITranslationDesk) : BasePresenter<ITranslationDesk>(mView) {
    /**
     * 创建控制数据
     * @param isOldModel 是否旧指令模式
     * true: 旧的指令模式（没有适配器的）
     * false: 新的指令模式（有适配器的）
     */
    fun createControlData(isOldModel:Boolean = true) {
        if (isOldModel){
            initOldCommand()
        }else{
            initNewCommand()
        }
    }

    /**
     * 初始化旧（没有适配器的指令）
     *
     */
    private fun initOldCommand(){
        val list = mutableListOf<ControlBean>()
        list.add(ControlBean(true, "捷昌平移导台控制-旧指令(无适配器)"))

        val up = ControlBean(false, "上升(连动模式下会一直发指令)")
        up.command = MGSerialPort.instance.getJieCang().translationDesk().up()
        list.add(up)

        val down = ControlBean(false, "下降(连动模式下会一直发指令)")
        down.command = MGSerialPort.instance.getJieCang().translationDesk().down()
        list.add(down)

        val stop = ControlBean(false, "停止")
        stop.command = MGSerialPort.instance.getJieCang().translationDesk().stop()
        list.add(stop)

        val scope = ControlBean(false, "行程范围")
        scope.command = MGSerialPort.instance.getJieCang().translationDesk().routeScope()
        list.add(scope)


        val height4 = ControlBean(false, "运行至4mm")
        height4.command = MGSerialPort.instance.getJieCang().translationDesk().runHeight(4)
        list.add(height4)

        val height150 = ControlBean(false, "运行至150mm")
        height150.command = MGSerialPort.instance.getJieCang().translationDesk().runHeight(150)
        list.add(height150)

        val height300 = ControlBean(false, "运行至300mm")
        height300.command = MGSerialPort.instance.getJieCang().translationDesk().runHeight(300)
        list.add(height300)

        val height450 = ControlBean(false, "运行至450mm")
        height450.command = MGSerialPort.instance.getJieCang().translationDesk().runHeight(450)
        list.add(height450)

        val height556 = ControlBean(false, "运行至556mm")
        height556.command = MGSerialPort.instance.getJieCang().translationDesk().runHeight(556)
        list.add(height556)


        for (index in 1 until 5) {
            val setJY1 = ControlBean(false, "设置当前位置为记忆${index}")
            setJY1.command =
                MGSerialPort.instance.getJieCang().translationDesk().setLocalityPosition(index)
            list.add(setJY1)
        }

        for (index in 1 until 5) {
            val runJY1 = ControlBean(false, "运行到记忆位置${index}")
            runJY1.command =
                MGSerialPort.instance.getJieCang().translationDesk().runLocalityPosition(index)
            list.add(runJY1)
        }


        val setUPLimit = ControlBean(false, "设置当前为上限位")
        setUPLimit.command = MGSerialPort.instance.getJieCang().translationDesk().setUpLimit()
        list.add(setUPLimit)

        val setDownLimit = ControlBean(false, "设置当前为下限位")
        setDownLimit.command = MGSerialPort.instance.getJieCang().translationDesk().setDownLimit()
        list.add(setDownLimit)

        val delLimit = ControlBean(false, "删除所有限位")
        delLimit.command = MGSerialPort.instance.getJieCang().translationDesk().deleteLimit()
        list.add(delLimit)

        val delUpLimit = ControlBean(false, "删除上限位")
        delUpLimit.command = MGSerialPort.instance.getJieCang().translationDesk().deleteUpLimit()
        list.add(delUpLimit)

        val delDownLimit = ControlBean(false, "删除下限位")
        delDownLimit.command =
            MGSerialPort.instance.getJieCang().translationDesk().deleteDownLimit()
        list.add(delDownLimit)

        val queryLimit = ControlBean(false, "查询限位状态")
        queryLimit.command = MGSerialPort.instance.getJieCang().translationDesk().queryLimitState()
        list.add(queryLimit)

        val queryUpLimit = ControlBean(false, "查询上限位情况")
        queryUpLimit.command = MGSerialPort.instance.getJieCang().translationDesk().queryUpLimit()
        list.add(queryUpLimit)

        val queryDownLimit = ControlBean(false, "查询下限位情况")
        queryDownLimit.command =
            MGSerialPort.instance.getJieCang().translationDesk().queryDownLimit()
        list.add(queryDownLimit)

        mIView.initControlList(list)
    }

    /**
     * 初始化新指令（有适配器模式下的指令）
     */
    private fun initNewCommand(){
        val list = mutableListOf<ControlBean>()
        list.add(ControlBean(true, "捷昌平移导台控制-新指令(有适配器)"))

        val local = ControlBean(false, "获取当前位置（当前高度）")
        local.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().getLocation()
        list.add(local)

        val scope = ControlBean(false, "行程范围")
        scope.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().getRouteScope()
        list.add(scope)


        val above = ControlBean(false, "获取上高度")
        above.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().getAboveHeight()
        list.add(above)

        val below = ControlBean(false, "获取下高度")
        below.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().getBelowHeight()
        list.add(below)

        val limit = ControlBean(false, "获取限位信息")
        limit.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().getLimitInfo()
        list.add(limit)

        val status = ControlBean(false, "获取设备当前状态")
        status.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().getExceptionInfo()
        list.add(status)

        val up = ControlBean(false, "上升")
        up.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().up()
        list.add(up)

        val down = ControlBean(false, "下降")
        down.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().down()
        list.add(down)

        val stop = ControlBean(false, "停止")
        stop.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().stop()
        list.add(stop)

        val stopAtOnce = ControlBean(false, "急停")
        stopAtOnce.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().stopAtOnce()
        list.add(stopAtOnce)

        val run5mm = ControlBean(false, "运行到5mm")
        run5mm.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().run(5)
        list.add(run5mm)

        val run50mm = ControlBean(false, "运行到50mm")
        run50mm.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().run(50)
        list.add(run50mm)

        val run150mm = ControlBean(false, "运行到150mm")
        run150mm.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().run(150)
        list.add(run150mm)

        val run300mm = ControlBean(false, "运行到300mm")
        run300mm.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().run(300)
        list.add(run300mm)

        val run450mm = ControlBean(false, "运行到450mm")
        run450mm.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().run(450)
        list.add(run450mm)

        val run500mm = ControlBean(false, "运行到500mm")
        run500mm.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().run(500)
        list.add(run500mm)

        val entryReset = ControlBean(false, "进入复位状态")
        entryReset.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().entryResetStatus()
        list.add(entryReset)

        val startReset = ControlBean(false, "开始复位")
        startReset.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().startReset()
        list.add(startReset)

        val stopReset = ControlBean(false, "停止复位")
        stopReset.command = MGSerialPort.instance.getJieCang().translationAdapterDesk().stopReset()
        list.add(stopReset)


        mIView.initControlList(list)
    }

    /**
     * 解析高度
     */
    fun parseHeight(command: ByteArray) {
        if (command.size < 6) return
        val check = byteArrayOf(0xF2.toByte(), 0xF2.toByte(), 0x01, 0x03)
        val start = ByteArray(4)
        System.arraycopy(command, 0, start, 0, 4)
        if (start.contentEquals(check)) {
            val height = ByteArray(2)
            System.arraycopy(command, 4, height, 0, 2)
            mIView.deviceHeightMM(height.toUInt16H())
        }
    }

    /**
     * 解析行程范围
     */
    fun parseScope(command: ByteArray) {
        if (command.size < 8) return
        val check = byteArrayOf(0xF2.toByte(), 0xF2.toByte(), 0x07, 0x04)
        val start = ByteArray(4)
        System.arraycopy(command, 0, start, 0, 4)
        if (start.contentEquals(check)) {
            val heightH = ByteArray(2)
            System.arraycopy(command, 4, heightH, 0, 2)
            val heightL = ByteArray(2)
            System.arraycopy(command, 6, heightL, 0, 2)
            mIView.deviceHeightScopeMM(heightH.toUInt16H(), heightL.toUInt16H())
        }
    }

    /*用来接受限制高度返回的质量*/
    private var limitH = ByteArray(0)

    /**
     * 解析限制高度
     */
    fun parseLimitHeight(command: ByteArray) {
        val total = ByteArray(limitH.size + command.size)
        if (limitH.isNotEmpty()) {
            System.arraycopy(limitH, 0, total, 0, limitH.size)
        }
        if (command.isNotEmpty()) {
            System.arraycopy(command, 0, total, limitH.size, command.size)
        }
        limitH = ByteArray(total.size)
        System.arraycopy(total, 0, limitH, 0, total.size)

        if (total.size < 7) return
        val check = byteArrayOf(0xF2.toByte(), 0xF2.toByte(), 0x20, 0x01)
        val start = ByteArray(4)
        System.arraycopy(total, 0, start, 0, 4)
        if (start.contentEquals(check)) {
            //说明是查询限位指令的
            when (total[4]) {
                0x00.toByte() -> {
                    //表示未设置上下限位
                    mIView.noneSettingLimit()
                }
                0x01.toByte() -> {
                    //表示设置了上限位 7 + 8
                    if (total.size == 15) {
                        val upH = ByteArray(2)
                        System.arraycopy(total, 11, upH, 0, 2)
                        mIView.limitDownLimit(upH.toUInt16H())
                    }
                }
                0x10.toByte() -> {
                    //表示设置了下限位 7 + 8
                    if (total.size == 15) {
                        val downH = ByteArray(2)
                        System.arraycopy(total, 11, downH, 0, 2)
                        mIView.limitDownLimit(downH.toUInt16H())
                    }
                }
                0x11.toByte() -> {
                    //表示设置了上限位、下限位 7 + 8 + 8
                    if (total.size == 23) {
                        val upH = ByteArray(2)
                        System.arraycopy(total, 11, upH, 0, 2)
                        val downH = ByteArray(2)
                        System.arraycopy(total, 19, downH, 0, 2)
                        mIView.limitUpLimit(upH.toUInt16H())
                        mIView.limitDownLimit(downH.toUInt16H())
                    }
                }
            }
        }
    }

    /**
     * 推出解析限制高度
     */
    fun exitInitHeightMode() {
        limitH = ByteArray(0)
    }
}